package logic

import (
	"context"
	"product/productrpc/productrpc"

	"product/api/internal/svc"
	"product/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAddLogic {
	return &UserAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserAddLogic) UserAdd(req *types.UserAddRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	add, err := l.svcCtx.ProductRpc.UserAdd(l.ctx, &productrpc.UserAddRequest{
		Img:   req.Img,
		Name:  req.Name,
		Price: float32(req.Price),
		Num:   req.Num,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Data:    nil,
			Code:    "500",
		}, nil
	}

	return &types.Response{
		Message: "添加成功",
		Data:    add,
		Code:    "200",
	}, nil
}
